<script type="text/javascript">
$(document).ready(function() {
        $("#reseller_price").hide();
        $("#divLoading").hide();
        $("#divLoadingSave").hide();
		
		$("#selltype").show();
        
        $('#fnClickAddRow').click(function() {
                var oTable = $('#records').dataTable(); 
                oTable.fnDraw();
                aler('OK');
        });
		
		$('#sales_affected').change(function() {
                var nilai = $( this ).val();
				if( nilai == 9 ){
						$("#selltype").show();
				}else {
						$("#selltype").hide();
				}
        });
        
        $('.d_product').change(function() {
            var item_id        = $(this).val();
            
            $("#divLoading").show();
            $.post(
                '<?=base_url()?>index.php/product/getProduct', 
                {item_id:item_id},
                function(data) {
                        $("#divLoading").hide();
                        if(data.status == 'OK') {
                                document.getElementById("divKode").innerHTML = data.kode;
                                document.getElementById("divGroup").innerHTML = data.group;
                                document.getElementById("divNama").innerHTML = data.nama;
                                document.getElementById("divHarga").innerHTML = data.harga;
                                document.getElementById("divHarga2").innerHTML = data.harga2;
                            
                                $("#reseller_price").show();
                                $("#divHarga").hide();
                        }
                },
                "json"
            ).error(function() {
                alert("-");
            });
        });
        
        $('.d_save').click(function() {
                var item_code              = $("#product").val();
                var pricing_date           = $("#productdate").val();
                var retail_price           = $("#divHarga").text();
                var condition_type         = $("#condition_type").val();
                var condition_value        = $("#reseller_price").val();
                var sales_affected         = $("#sales_affected").val();
                var item_group             = $("#divGroup").text();
                var item_name              = $("#divNama").text();
				var selling_type		   = $("#selling_type").val();
                
                $("#divLoadingSave").show();
                
                $.post(
                        '<?=base_url()?>index.php/product/saveProduct', 
                        {item_code:item_code,pricing_date:pricing_date,retail_price:retail_price,condition_type:condition_type,condition_value:condition_value,sales_affected:sales_affected,item_group:item_group,item_name:item_name,selling_type:selling_type},
                        function(data) {
                                $("#divLoadingSave").hide();
                                if(data.status == 'ERROR') {
                                        $("#save").show();
                                        alert(data.pesan);
                                }else if(data.status == 'OK') {
                                        document.getElementById("divPesan").innerHTML = data.pesan;
                                        
                                        document.getElementById("divKode").innerHTML = "";
                                        document.getElementById("divGroup").innerHTML = "";
                                        document.getElementById("divNama").innerHTML = "";
                                        document.getElementById("divHarga").innerHTML = "";
                                        document.getElementById("divHarga2").innerHTML = "";
                                        document.getElementById("divHargaBaru").innerHTML = "";
                                        
                                        $("#product").val("");
                                        $("#reseller_price").val("");
                                        //$("#reseller_price").val("");
                                        /*$('#records').dataTable().fnAddData( [
                                                data.no,
                                                data.item_code,
                                                data.item_group,
                                                data.default_price,
                                                data.reseller_price,
                                                data.default_price,
                                                data.reseller_price ] );*/
                                }
                        },
                        "json"
                ).error(function() {
                        alert("-");
                });
        });
        
        $('#reseller_price').keyup(function() {
            var retail_price   = $("#divHarga").text();
	    var condition_type = $("#condition_type").val();
	    var condition_value= $(this).val();
            
            $.post(
                '<?=base_url()?>index.php/product/countResellerPrice', 
                {retail_price:retail_price,condition_type:condition_type,condition_value:condition_value},
                function(data) {
                    if(data.status == 'OK') {
                        document.getElementById("divHargaBaru").innerHTML = data.pesan;
                        document.getElementById("divPesanError").innerHTML = "";
                    }else{
                        document.getElementById("divHargaBaru").innerHTML = "";
                        document.getElementById("divPesanError").innerHTML = data.pesan;
                    }
                },
                "json"
            ).error(function() {
                alert("-");
            });
        });
});

    $(document).ready(function() {
        $("#datepicker").datepicker({dateFormat: 'yy-mm-dd'});
        $("#productdate").datepicker({ dateFormat: "yy-mm-dd",
                        minDate: new Date(<?=date('Y')?>, <?=date('m')?>-1, <?=date('d')?>),
                        showAnim: "slideDown"
        });
      });                 
</script>
<div class="box">
        <!-- box / title -->
        <div class="title">
                <h5>Manage Price</h5>
        </div>
        <!-- end box / title -->
        
                <div id="divPesan"></div>
                <form id="form" action="<?=$_SERVER['PHP_SELF']?>" method="post">
                <div class="form">
                        <div class="fields">
                                <div class="field">
                                        <div class="label">
                                                <label for="datepicker">Date:</label>
                                        </div>
                                        <div class="input">
                                                <input type="text" name="datepicker" id="productdate" class="small" value="<?=date('Y-m-d')?>">
                                        </div>
                                </div>
                                <div class="field">
                                        <div class="label" style="padding:0px 0px 0px 5px;">
                                                <label for="terr">Territory:</label>
                                        </div>
                                        <div class="input">
                                                <b><?=$territory_name?>&nbsp;</b>
                                        </div>
                                </div>
                                <div class="field">
                                        <div class="label">
                                                <label for="product">Product:</label>
                                        </div>
                                        <div class="input">
                                                <select name="product" id="product" class="d_product">
                                                        <option value=''>Select Product</option>
                                                        <?php
                                                        $prod = $this->mproduct->getList();
                                                        foreach($prod as $r){
                                                                echo'<option value="'. $r->mvalue .'">'. $r->mdisplay .'</option>';
                                                        }
                                                        ?>
                                                </select> <div id="divLoading"><img src="<?=base_url()?>file/images/loading.gif"></div>
                                        </div>
                                </div>
                                <div class="field">
                                        <div class="label" style="padding:0px 0px 0px 5px;">
                                                <label for="code">Product Code:</label>
                                        </div>
                                        <div class="input">
                                                <b><div id="divKode"></div>&nbsp;</b>
                                        </div>
                                </div>
                                <div class="field">
                                        <div class="label" style="padding:0px 0px 0px 5px;">
                                                <label for="code">Product Group:</label>
                                        </div>
                                        <div class="input">
                                                <b><div id="divGroup"></div>&nbsp;</b>
                                        </div>
                                </div>
                                <div class="field">
                                        <div class="label" style="padding:0px 0px 0px 5px;">
                                                <label for="name">Product Name:</label>
                                        </div>
                                        <div class="input">
                                                <b><div id="divNama"></div>&nbsp;</b>
                                        </div>
                                </div>
                                <div class="field">
                                        <div class="label" style="padding:0px 0px 0px 5px;">
                                                <label for="defp">Retail Price:</label>
                                        </div>
                                        <div class="input">
                                                <b><div id="divHarga"></div><div id="divHarga2"></div>&nbsp;</b>
                                        </div>
                                </div>
                                <div class="field">
                                        <div class="label" style="padding:0px 0px 0px 5px;">
                                                <label for="defp">Sales Affected:</label>
                                        </div>
                                        <div class="input">
                                                <select name='sales_affected' id='sales_affected'>
                                                <?php
                                                foreach($SalesAffected as $r){
                                                        echo'<option value="'. $r->member_value .'">'. $r->member_display .'</option>';
                                                }
                                                ?>
                                                </select>
                                        </div>
                                </div>
								
								<div class="field" id="selltype">
                                        <div class="label" style="padding:0px 0px 0px 5px;">
                                                <label for="defp">Selling Type:</label>
                                        </div>
                                        <div class="input">
                                                <select name='selling_type' id='selling_type'>
                                                <?php
												$getSellingType=$this->mproduct->getSellingType();
                                                foreach($getSellingType as $r){
                                                        echo'<option value="'. $r->member_value .'">'. $r->member_display .'</option>';
                                                }
                                                ?>
                                                </select>
                                        </div>
                                </div>
								
                                <div class="field">
                                        <div class="label" style="padding:0px 0px 0px 5px;">
                                                <label for="defp">Condition Type:</label>
                                        </div>
                                        <div class="input">
                                                <select name='condition_type' id="condition_type">
                                                <?php
                                                foreach($ConditionType as $r){
                                                        echo'<option value="'. $r->member_value .'">'. $r->member_display .'</option>';
                                                }
                                                ?>
                                                </select>
                                        </div>
                                </div>
                                <div class="field">
                                        <div class="label" style="padding:0px 0px 0px 5px;">
                                                <label for="defp">Condition Value:</label>
                                        </div>
                                        <div class="input">
                                                <input type="text" id="reseller_price" name="reseller_price" class="small" /> <div id="divPesanError"></div> &nbsp;
                                        </div>
                                </div>
                                <div class="field">
                                        <div class="label" style="padding:0px 0px 0px 5px;">
                                                <label for="keterangan">Reseller Price:</label>
                                        </div>
                                        <div class="input">
                                                <b><div id="divHargaBaru" style="font-size:14px;color:red;"></div>&nbsp;</b>
                                        </div>
                                </div>
                                <div class="buttons">
                                        <input type="button" name="submit" value="Save Pricing" id="save" class="d_save" />
                                        <div id="divLoadingSave"><img src="<?=base_url()?>file/images/loading.gif"></div>
                                </div>
                        </div>
                </div>
                </form><br><br>&nbsp;        
       
</div>